1. Field of the Invention
This invention relates to a method and apparatus for interpreting and executing JavaScript programs, such as JavaScript programs embedded in hypertext markup language (xe2x80x9cHTMLxe2x80x9d) documents. More particularly, embodiments of the present invention relate to a method and apparatus for a JavaScript interpreter written in Java. Specifically, a JavaScript interpreter may intercept JavaScript source code within HTML documents, parse and convert the JavaScript source code to a proprietary intermediate form represented by objects written in Java. The JavaScript interpreter may interface with JavaScript library objects to execute the intermediate representation equivalent of the original JavaScript source code.
2. Description of the Related Art
JavaScript is a Web scripting language developed by Netscape Communications Corporation. JavaScript was one of the first Web scripting languages and it remains one of the most popular today. (Please note that JAVA and JAVASCRIPT used herein are registered trademarks of Sun Microsystems, Inc.) JavaScript may be directly included in HTML documents. HTML is a language used to create Web documents. Referring now to FIG. 1, the JavaScript source code is typically embedded in HTML documents 110, by using a script tag, such as xe2x80x9c less than SCRIPT LANGUAGE=JavaScript greater than xe2x80x9d. A Web browser 120, such as Netscape Navigator, executes HTML documents containing JavaScript source code. Unlike compiled languages, JavaScript is an interpreted language. This means the Web browser executes each line of JavaScript as it comes to it. The ability to interpret and execute JavaScript source code has been provided in popular Web browsers, such as Netscape Navigator and Microsoft Internet Explorer. JavaScript programs run within a Web browser or other JavaScript-enabled applications to produce the desired results 130. The JavaScript language is regularly updated by Netscape. The most recent version of JavaScript is version 1.3.
Typical prior art Web browsers that support JavaScript are programmed in native code complied for a particular computer platform. However, it may be desirable to support JavaScript in a Web browser programmed in a platform-independent language such as Java. A Java-based Web browser may run on any computing platform under a Java Virtual Machine for the particular platform. However, Java-based programs typically execute more and may have a larger memory footprint that natively coded programs. Thus, adding JavaScript support to a Java-based browser may exacerbate speed and memory drawbacks. It may be desirable to overcome these drawbacks for a Java-based browser with JavaScript support, especially for use in embedded devices such as personal digital assistants (PDAs), wireless communication devices, such as cellular telephones, and other small consumer devices where computer processing speed and memory resources are at a premium. The present invention may provide a method and apparatus for interpreting and executing JavaScript source code embedded in HTML documents. The interpreter of the present invention may be written using the Java programming language. The present invention uses a smaller memory footprint than the prior art in interpreting and executing JavaScript programs. Furthermore, the architecture of the present invention may enable faster execution of JavaScript programs and enable the use of independent implementations of the interpreter, the Web browser and the JavaScript object library.
A method and apparatus is disclosed for interfacing a JavaScript interpreter with a JavaScript library of host objects implemented using the Java programming language. Specifically, a method and apparatus is disclosed for accessing a JavaScript program, parsing the program, generating an intermediate representation of the JavaScript program, and executing the intermediate representation by interfacing with the JavaScript library of host objects.
In the preferred embodiment of the present invention, the JavaScript program is embedded in HTML documents in an Internet Web browser. The Web browser is programmed to intercept the JavaScript code and pass execution control over the program to an interpreter engine developed using the Java programming language. The parsing component of the engine validates the JavaScript instructions. The validated instructions are converted by the code generator component of the engine into an intermediate representation equivalent in Java code. The interpreter engine accesses the program""s library of host objects through an interface to the library and executes the intermediate representation to produce the desired results as programmed in the original JavaScript source program. The implementation of the interface enables the implementation of the interpreter engine to be independent from the implementation of the library of host objects and independent from the implementation of the Web browser. The library of host objects is also implemented using the Java programming language, where the host objects are derived from abstract Java classes. The host objects of the library contain fields and methods identified in the JavaScript source program. The interface dynamically updates the host objects of the library. The dynamic update capability of the interface is implemented using hash tables. Methods previously unknown to the library are stored in the hash tables in their original source code syntax. The present invention further contemplates information media that conveys software for implementing the method disclosed herein.